<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="开课院系">
              <a-select allowClear v-model="queryParam.pydw" @change="yxChange" showSearch :filterOption="filterOption">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="课程类别">
              <a-select allowClear v-model="queryParam.kclb" @change="lbChange">
                <a-select-option v-for="i in TYPE0359" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="课程名称">
                <a-select allowClear v-model="queryParam.kcbh" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.kc" :key="i.id" :value="i.kch">{{i.kch}}-{{ i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学年">
                <a-select allowClear v-model="queryParam.kkxn">
                  <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课学期">
                <a-select allowClear v-model="queryParam.kkxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="开课年级">
                <a-select allowClear v-model="queryParam.kknj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" style="margin:0 10px" @click="exportExcel">导出</a-button>
            <a-button @click="queryParam = {};$refs.table.refresh()">重置</a-button>

            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="{show: true}"
        :pageSize="pageSize"
      >
        <template slot="operation" slot-scope="a,b">
          <a>修改</a>
        </template>
      </s-table>
    </a-card>

  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '学号',
          dataIndex: 'xsxh'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '所属院系',
          dataIndex: 'dwmc'
        },
        {
          title: '所属专业',
          dataIndex: 'zymc'
        },
        {
          title: '课程名称',
          dataIndex: 'kcmc'
        },
        {
          title: '修课状态',
          dataIndex: 'xkztmc'
        }
      ],
      loadData: parameter => {
        if (this.pageSize !== 10) {
          parameter.pageSize = this.pageSize
        } else {
          this.pageSize = parameter.pageSize
        }
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.attendStudentList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      pageSize: 10,
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'], // 学年
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0359: state => state.app['TYPE0359'] // 课程类别
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.$set(this.queryParam, 'kkxq', res[1].currentSemester)
        this.$set(this.queryParam, 'kkxn', res[1].schoolYear)
        this.$set(this.queryParam, 'kknj', res[1].currentGrade)
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'kcbh', '')
      if (code) {
        this.$api.cultivate.courseList({kcssyxm: code, kclbm: this.queryParam.kclb}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data.rows)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    lbChange (code) {
      this.$set(this.queryParam, 'kcbh', '')
      if (code) {
        this.$api.cultivate.courseList({kcssyxm: this.queryParam.pydw, kclbm: code}).then(res => {
          this.$set(this.selectMenu, 'kc', res.data.rows)
        })
      } else {
        this.$set(this.selectMenu, 'kc', [])
      }
    },
    exportExcel () {
      let params = this.queryParam
      this.$export('/foster/tkxsmd/tkxsmdExport', {
        ...params
      })
    }
  }
}
</script>

<style>

</style>
